package database.dao;

import android.content.Context;

import com.ma.App;


import utils.ConstantUtil;


import utils.SharedPrefUtil;


/**
 * Created by Administrator on 2016/9/13 0013.
 */
public class DBManager {




    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private Context context;

    private DBManager(Context context,String dbName) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @return
     */
     public static DBManager getInstance() {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                String url   = "wyy_p2";
//                String url   = SharedPrefUtil.getInstance().getString(ConstantUtil.URL);
                init(url);

            }
        }
        return mInstance;
    }


    /**
     * 获取单例引用
     *
     * @return
     */
   public static void init(String url) {
       String dbName   = url.replace("/", "").replace(":", "").replace(".", "");
       mInstance = new DBManager(App.getInstance(),dbName);


    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(openHelper.getWritableDatabase());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    public  static  DaoSession get(){
      return   DBManager.getInstance().getDaoSession();
    }
}
